//
//  SceneryViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/6.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "SceneryViewController.h"
#import "DetailSceneryViewController.h"
#import "WeatherViewController.h"
#import "ControllerManager.h"

#define CITY_NAME_PIC @"泰国",@"越南",@"马来西亚",@"新加坡",@"柬埔寨",@"缅甸",@"老挝",@"文莱",@"印度尼西亚",@"菲律宾"
#define THAILAND_CITY_NAME @"清迈",@"曼谷",@"普吉岛",@"芭提雅",@"苏梅岛"
#define VIETNAM_CITY_NAME @"胡志明市",@"岘港",@"芽庄",@"沙巴",@"河内"
#define MALAYSIA_CITY_NAME @"吉隆坡",@"沙巴",@"南卡威",@"槟城",@"仙本那"
#define SINGAPORE_CITY_NAME @"圣淘沙",@"环球影城",@"鱼尾狮公园",@"海底世界",@"滨海湾花园"
#define CAMBODIA_CITY_NAME @"吴哥窟",@"金边",@"暹粒",@"马德望"
#define MYANMAR_CITY_NAME @"仰光",@"曼德勒",@"蒲甘"
#define LAOS_CITY_NAME @"琅勃拉邦",@"万荣",@"万象",@"丰沙湾"
#define BRUNEI_CITY_NAME @"斯里巴加湾市"
#define INDONESIA_CITY_NAME @"巴厘岛",@"雅加达",@"日惹",@"婆罗浮屠",@"巴布亚岛"
#define PHILIPPINES_CITY_NAME @"马尼拉",@"长滩岛",@"海豚湾",@"巴拉望",@"薄荷岛"


@interface SceneryViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *sceneryTableView;
@property (nonatomic, strong) NSMutableArray *sceneryPhotoArray;
@property (nonatomic, strong) UIImageView *titleView;
@property (nonatomic, strong) CALayer *backgroundLayer;
@property (nonatomic, assign) NSInteger lastIndex;
@property (nonatomic, strong) NSMutableArray *currentArray;
@property (nonatomic, strong) NSArray *rowsInSectionArray;
@property (nonatomic, strong) UIButton *drawerButton;



@end

@implementation SceneryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.sceneryTableView];
    [self.view addSubview:self.drawerButton];
    [self initWithArray];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.currentArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.currentArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = self.rowsInSectionArray[indexPath.section][indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = [UIFont fontWithName:@"Courier-Bold" size:20];
    cell.textLabel.textColor = [UIColor colorWithRed:1.000 green:0.874 blue:0.018 alpha:1.000];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 230;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, self.view.frame.size.width, 230);
    [button setBackgroundImage:[UIImage imageNamed:self.sceneryPhotoArray[section]] forState:UIControlStateNormal];
    button.tag = section;
    [button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:button];
    return headerView;
}

#pragma mark - privte methods
- (void)buttonTaped:(UIButton *)sender
{
    if ([self.currentArray[sender.tag]count] > 0) {
        [self.currentArray[_lastIndex] removeAllObjects];
        [self.sceneryTableView reloadSections:[NSIndexSet indexSetWithIndex:_lastIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else
    {
        // 通过不断删除和添加来实现展开和收起
        [[self.currentArray objectAtIndex:_lastIndex] removeAllObjects];
        [self.sceneryTableView reloadSections:[NSIndexSet indexSetWithIndex:_lastIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        [self.currentArray[sender.tag] addObjectsFromArray:self.rowsInSectionArray[sender.tag]];
        [self.sceneryTableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
        _lastIndex = sender.tag;
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:sender.tag];
        [self.sceneryTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

- (void)initWithArray
{
    for (int i = 0; i < self.sceneryPhotoArray.count; i ++) {
        NSMutableArray* sections = [[NSMutableArray alloc]init];
        [self.currentArray addObject:sections];
    }
}

- (void)drawerButtonTaped:(UIButton *)sender
{
    [[ControllerManager sharedManager] leftDrawerOpen:sender];
}
#pragma mark -- UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailSceneryViewController *detailSceneryVC = [[DetailSceneryViewController alloc]init];
    detailSceneryVC.city = self.currentArray[indexPath.section][indexPath.row];
    detailSceneryVC.indexOfSection = indexPath.section;
    [self.navigationController pushViewController:detailSceneryVC animated:YES];
}

#pragma mark -- getter

- (UIView *)titleView
{
    if (!_titleView) {
        _titleView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
        _titleView.image = [UIImage imageNamed:@"23456"];
    }
    return _titleView;
}

- (UITableView *)sceneryTableView
{
    if (!_sceneryTableView) {
        _sceneryTableView = ({
            UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.titleView.frame), self.view.bounds.size.width, self.view.bounds.size.height - self.titleView.bounds.size.height) style:UITableViewStylePlain];
            tableView.rowHeight = 30;
            tableView.dataSource = self;
            tableView.delegate = self;
            [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cityCell"];
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            tableView.backgroundColor = [UIColor whiteColor];
            tableView.showsVerticalScrollIndicator = NO;
            tableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell背景图3"]];
            tableView;
        });
    }
    return _sceneryTableView;
}

- (NSMutableArray *)sceneryPhotoArray
{
    if (!_sceneryPhotoArray) {
        //泰国、越南、马来西亚、新加坡、柬埔寨、缅甸、老挝、文莱、印度尼西亚、菲律宾、东帝汶
        _sceneryPhotoArray = [NSMutableArray arrayWithObjects:CITY_NAME_PIC, nil];
    }
    return _sceneryPhotoArray;
}



- (CALayer *)backgroundLayer
{
    if (!_backgroundLayer) {
        _backgroundLayer = [CALayer layer];
        _backgroundLayer.contents = (__bridge id)[UIImage imageNamed:@"cell背景图3.png" ].CGImage;
        _backgroundLayer.frame = CGRectMake(0, self.titleView.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height - self.titleView.bounds.size.height);
    }
    return _backgroundLayer;
}

- (NSArray *)rowsInSectionArray
{
    if (!_rowsInSectionArray) {
        _rowsInSectionArray = @[@[THAILAND_CITY_NAME],@[VIETNAM_CITY_NAME],@[MALAYSIA_CITY_NAME],@[SINGAPORE_CITY_NAME],@[CAMBODIA_CITY_NAME],@[MYANMAR_CITY_NAME],@[LAOS_CITY_NAME],@[BRUNEI_CITY_NAME],@[INDONESIA_CITY_NAME],@[PHILIPPINES_CITY_NAME]];
    }
    return _rowsInSectionArray;
}

- (NSMutableArray *)currentArray
{
    if (!_currentArray) {
        _currentArray = [NSMutableArray array];
    }
    return _currentArray;
}

- (UIButton *)drawerButton
{
    if (!_drawerButton) {
        _drawerButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(CGRectGetMinX(self.view.bounds),15, 65, 40);
            [button setBackgroundImage:[UIImage imageNamed:@"个人中心按钮.png"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(drawerButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _drawerButton;
}
@end
